//
//  LDWelcomeViewController.swift
//  Weibo201655
//
//  Created by pro on 16/5/6.
//  Copyright © 2016年 LeeDan. All rights reserved.
//

import UIKit
import SDWebImage
import SnapKit

class LDWelcomeViewController: UIViewController {
    
    //iconview的底部约束
    var iconviewBottomConstraint: NSLayoutConstraint?

    override func viewDidLoad() {
        super.viewDidLoad()
        
        setupUI()
        
        //请求头像
        if let urlStr = sharedUserAccount?.avatar_large {
            iconView .sd_setImageWithURL(NSURL(string: urlStr), placeholderImage: UIImage(named: "avatar_default_big"), options: SDWebImageOptions.RetryFailed)
        }
        //显示昵称
        guard let name = sharedUserAccount?.screen_name else {
            print("昵称没有值哦")
            return
        }
        titleLabel.text = name
    }

    func setupUI() {
        //添加子控件
        view.addSubview(bgImageView)
        view.addSubview(iconView)
        view.addSubview(titleLabel)
        
        //添加约束
        setupConstraint()
    }
    
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        
        //iconview动画
        startIconViewAnimation()
    }
    //MARK: iconview动画
    private func startIconViewAnimation() {
    
//        iconviewBottomConstraint?.constant = -(view.frame.height - 160)
        //更新约束
        iconView.snp_updateConstraints { (make) -> Void in
            make.bottom.equalTo(view).offset(-(view.frame.height - 160))
        }
        
        UIView.animateWithDuration(1.25, delay: 0.1, usingSpringWithDamping: 0.5, initialSpringVelocity: 6, options: UIViewAnimationOptions.CurveEaseInOut, animations: { () -> Void in
            //更新约束
            self.view.layoutIfNeeded()
            
            }) { (_) -> Void in
                //显示文字
                UIView.animateWithDuration(0.5, animations: { () -> Void in
                    //显示文字
                    self.titleLabel.alpha = 1.0
                    }, completion: { (_) -> Void in
                
                     //切换控制器
                     NSNotificationCenter.defaultCenter().postNotificationName(LDSwitchRootViewControllerNotification, object: false)
                })
        }
    
    }
    
    //MARK: 添加约束
    func setupConstraint() {
        
//        bgImageView.translatesAutoresizingMaskIntoConstraints = false
//        iconView.translatesAutoresizingMaskIntoConstraints = false
//        titleLabel.translatesAutoresizingMaskIntoConstraints = false
        
        //bgImageView
//        view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[bgImageView]-0-|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: ["bgImageView": bgImageView]))
//        view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[bgImageView]-0-|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: ["bgImageView": bgImageView]))
        
        bgImageView.snp_makeConstraints { (make) -> Void in
            make.edges.equalTo(EdgeInsetsZero)//填充父控件
        }
        
        //iconView
//        view.addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0.0))
//        
//        iconviewBottomConstraint = NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: -160)
//        view.addConstraint(iconviewBottomConstraint!)
//        //宽高
//        view.addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 85))
//        view.addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 85))
        
        iconView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(view)
            make.bottom.equalTo(view.snp_bottom).offset(-160)
            make.size.equalTo(CGSizeMake(85, 85))
        }
        
        //titleLabel
//        view.addConstraint(NSLayoutConstraint(item: titleLabel, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0.0))
//        view.addConstraint(NSLayoutConstraint(item: titleLabel, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 16))
        
        titleLabel.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(view)
            make.top.equalTo(iconView.snp_bottom).offset(16)
        }
        
    }
    
    //MARK: lazy
    //背景图
    lazy var bgImageView: UIImageView = UIImageView(image: UIImage(named: "ad_background"))
    
    //头像
    lazy var iconView:UIImageView = {
    
        let imgView = UIImageView(image: UIImage(named: "avatar_default_big"))
        
        //切圆
        imgView.layer.cornerRadius = imgView.frame.width * 0.5
        imgView.layer.masksToBounds = true
        
        return imgView
    }()
    
    //标题
    lazy var titleLabel: UILabel = {
        let label = UILabel()
        label.text = "欢迎归来"
        label.textAlignment = NSTextAlignment.Center
        label.font = UIFont.systemFontOfSize(14)
        label.textColor = UIColor.darkGrayColor()
        //alpha
        label.alpha = 0
        return label
    }()
    
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

}
